package cc.comac.mom.datahandlers.entity;

import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import javax.xml.soap.*;
import java.io.IOException;
public class QarReturnSAPData {

	public static SOAPBody returnData(QualityQarCheckInvoiceEntity selectOne) throws SOAPException, IOException{
		String materialDocument = selectOne.getMaterialDocument();
		String materialDocumentItem = selectOne.getMaterialDocumentItem();
		String qarNumber = selectOne.getQarNo();
		String material = selectOne.getMaterialNumber();
		String batchNumber = selectOne.getFinishedProductSn();
		String serialNumber = selectOne.getSerialNumber();
		String qualifiedNumber = selectOne.getQualifiedQty() + "";
		String unqualifiedNumber = selectOne.getRejectionQty() + "";
		String qualifiedRate = selectOne.getPercentOfPass() + "";

		String myNamespace = "mom";
		String myNamespaceURI = "urn://www.comac.com/mom";

		// Create SOAP Connection
		SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

		SOAPConnection soapConnection = soapConnectionFactory.createConnection();
		// Create SOAP Message
		MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.1 Protocol");
		SOAPMessage soapMessage = messageFactory.createMessage();
		MimeHeaders hd = soapMessage.getMimeHeaders();
		hd.addHeader("Authorization", "Basic UElURVNUOmNvbWFjMTIz");
		SOAPPart soapPart = soapMessage.getSOAPPart();

		// SOAP Envelope
		SOAPEnvelope envelope = soapPart.getEnvelope();
		envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

		// SOAP Body
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyElem = soapBody.addChildElement("MT_MOM_QM_Request_Out", myNamespace);

		SOAPElement requestElem = soapBodyElem.addChildElement("QualityInspectionResult");

		SOAPElement brNoEle = requestElem.addChildElement("MaterialDocument");
		brNoEle.addTextNode(materialDocument);
		SOAPElement contentEle = requestElem.addChildElement("MaterialDocumentItem");
		contentEle.addTextNode(materialDocumentItem);
		SOAPElement reqDate = requestElem.addChildElement("QARNumber");
		reqDate.addTextNode(qarNumber);
		SOAPElement reqSerialEle = requestElem.addChildElement("Material");

		reqSerialEle.addTextNode(material);
		SOAPElement reqTime = requestElem.addChildElement("BatchNumber");
		reqTime.addTextNode(batchNumber);
		SOAPElement token = requestElem.addChildElement("SerialNumber");
		token.addTextNode(serialNumber);
		SOAPElement txCode = requestElem.addChildElement("QualifiedNumber");
		txCode.addTextNode(qualifiedNumber);
		SOAPElement aa = requestElem.addChildElement("UnqualifiedNumber");
		aa.addTextNode(unqualifiedNumber);
		SOAPElement bb = requestElem.addChildElement("QualifiedRate");
		bb.addTextNode(qualifiedRate);
		soapMessage.saveChanges();
		soapMessage.writeTo(System.out);
		// Send SOAP Message to SOAP Serve
		SOAPMessage soapResponse = soapConnection.call(soapMessage,
				"http://10.14.3.83/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MOM&receiverParty=&receiverService=&interface=SI_MOM_QM_Out&interfaceNamespace=urn://www.comac.com/mom");

		soapResponse.writeTo(System.out);
		// 拿到结果
		SOAPBody soapBodyRes = soapResponse.getSOAPBody();
		return soapBodyRes;
	}

}
